package com.kt.delivery.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kt.delivery.domain.Order;
import com.kt.delivery.domain.TakingOrder;
import com.kt.delivery.domain.dto.OtherOrderDto;
import com.kt.delivery.domain.dto.TakingOrderDto;
import com.kt.delivery.domain.vo.TakingOrderVo;
import com.kt.delivery.exception.ApiException;
import com.kt.delivery.mapper.OrderMapper;
import com.kt.delivery.mapper.TakingOrderMapper;
import com.kt.delivery.mapper.UserMapper;
import com.kt.delivery.service.TakingOrderService;
import com.kt.delivery.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor

public class TakingOrderServiceImpl implements TakingOrderService {

    private final TakingOrderMapper takingOrderMapper;

    private final UserMapper userMapper;

    private final LoginHelper loginHelper;

    private final OrderMapper orderMapper;

    /**
     * 接单员页面  代取件下代接订单列表
     * @param takingOrderDto
     * @return
     */
    @Override
    public List<TakingOrderVo> getTakeOrder(TakingOrderDto takingOrderDto) {
        return takingOrderMapper.getTakeOrder(takingOrderDto);
    }


    /**
     * 接单员页面  代取件下的（已接订单 配送订单 历史订单 的列表）
     * @param takingOrderDto
     * @return
     */
    @Override
    public List<TakingOrderVo> getTakeIdOrder(TakingOrderDto takingOrderDto , HttpServletRequest request) {

        // 获取属于接单员的用户id
        Long userId = loginHelper.getUserId();
        return takingOrderMapper.getTakeIdOrder(takingOrderDto, userId);
    }


    /**
     * 接单员页面  代寄下代接订单列表
     * @param otherOrderDto 参数
     * @return 结果
     */
    @Override
    public List<TakingOrderVo> getOthersOrder(OtherOrderDto otherOrderDto) {
        return takingOrderMapper.getOthersOrder(otherOrderDto);
    }


    /**
     * 接单员页面  代寄下（已接订单 配送订单 历史订单 的列表）
     * @param otherOrderDto 参数
     * @param
     * @return 结果
     */
    @Override
    public List<TakingOrderVo> getOthersStatusOrder(OtherOrderDto otherOrderDto) {
        Long userId = loginHelper.getUserId();
        return takingOrderMapper.getOthersStatusOrder(otherOrderDto,userId);
    }

    /**
     * 接单
     * @param orderId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int TakeOrders(String orderId) {
        // 拿到订单号修改订单状态为已接单

        LambdaUpdateWrapper<TakingOrder> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(TakingOrder::getStatus,20);
        // 接单人
        lambdaUpdateWrapper.set(TakingOrder::getTakingId,loginHelper.getUserId());
        lambdaUpdateWrapper.eq(TakingOrder::getOrderId,orderId);

        int update = takingOrderMapper.update(lambdaUpdateWrapper);

        if (update<0){
            throw new ApiException("接单失败","530");
        }

        // 再把用户订单状态修改成已取件
        LambdaUpdateWrapper<Order> orderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        orderLambdaUpdateWrapper.set(Order::getStatus,1);
        orderLambdaUpdateWrapper.eq(Order::getOrderId,orderId);

        int update1 = orderMapper.update(orderLambdaUpdateWrapper);

        if (update1<0){
            throw new ApiException("接单失败","330");
        }
        return 1;
    }

    /**
     * 开始派送
     * @param orderId 订单id
     * @return 结果
     */
    @Override
    public int Dispatch(String orderId) {

        LambdaUpdateWrapper<TakingOrder> takingOrderLambdaUpdateWrapper  = new LambdaUpdateWrapper<>();
        takingOrderLambdaUpdateWrapper.set(TakingOrder::getStatus,30);
        takingOrderLambdaUpdateWrapper.eq(TakingOrder::getOrderId,orderId);
        // 根据orderId修改接单员订单信息状态为开始派送
        int updateTakingOrder = takingOrderMapper.update(takingOrderLambdaUpdateWrapper);
        if (updateTakingOrder<0){
            throw new ApiException("派送异常，稍后重试！！！","651");
        }


        // TODO 根据orderId修改用户订单信息状态为正在派送
        return updateTakingOrder;
    }















}

